package com.tools.cleanmaster.ui.notification;


import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.RemoteViews;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.manager.NotificationSceneManager;
import com.tools.cleanmaster.ui.activity.SceneEmptyActivity;
import com.tools.cleanmaster.utils.CompatibilityHelper;

public class UIBuilder {
    public static final String EXTRA_REAL_INTENT = "real_intent";
    public static final String EXTRA_SCENE_TYPE = "scene_type";

    private NotificationBaseItem.UIParam mParam;

    public UIBuilder(NotificationBaseItem.UIParam param) {
        mParam = param;
    }

    @SuppressLint("NewApi")
    public Notification build() {
        Notification notification;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Context appContext = CleanApplication.getInstance();

            String notificationChannelId = appContext.getPackageName() + "notification_channel";
            String description = appContext.getPackageName();
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel channel = new NotificationChannel(notificationChannelId, description, importance);
            NotificationManager notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }

            PendingIntent intent = getContentIntent();
            NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext, notificationChannelId);
            builder.setAutoCancel(true);
            builder.setSmallIcon(mParam.tickerIcon);
            builder.setContentIntent(intent);
            builder.setDeleteIntent(getDeleteIntent());
            builder.setContent(getContentViews(intent));
            notification = builder.build();
        } else {
            notification = new Notification();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.icon = mParam.tickerIcon;
            notification.tickerText = getTickerText();
            notification.defaults = 0;
            notification.sound = null;
            notification.vibrate = null;
            notification.contentIntent = getContentIntent();
            notification.deleteIntent = getDeleteIntent();
            notification.contentView = getContentViews(notification.contentIntent);
        }

        return notification;
    }

    /**
     * @return tickerText
     */
    private CharSequence getTickerText() {
        if (TextUtils.isEmpty(mParam.tickerText)) {
            return mParam.title;
        } else {
            return mParam.tickerText;
        }
    }

    /**
     * @return contentIntent
     */
    private PendingIntent getContentIntent() {
        Context appContext = CleanApplication.getInstance();
        Intent intent = new Intent(appContext, SceneEmptyActivity.class);
        intent.setAction(String.valueOf(System.currentTimeMillis()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(EXTRA_REAL_INTENT, mParam.contentIntent);
        if (mParam.sceneType != null) {
            intent.putExtra(EXTRA_SCENE_TYPE, mParam.sceneType.key);
        }
        return PendingIntent
                .getActivity(appContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    /**
     * @return pendingIntent
     */
    private PendingIntent getDeleteIntent() {
        Intent intentD = new Intent(NotificationSceneManager.ACTION_SCENE_DELETE);
        return PendingIntent
                .getBroadcast(CleanApplication.getInstance(), 0, intentD, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    @SuppressLint("NewApi")
    private RemoteViews getContentViews(PendingIntent clickIntent) {
        Context appContext = CleanApplication.getInstance();
        RemoteViews remoteViews = new RemoteViews(appContext.getPackageName(), UIType.getView(mParam.viewType));
        remoteViews.setImageViewResource(R.id.notification_icon, mParam.leftIconResId);
        remoteViews.setTextViewText(R.id.notification_title, mParam.title);
        if (mParam.titleColor != 0) {
            remoteViews.setTextColor(R.id.notification_title, mParam.titleColor);
        }
        if (mParam.leftIconBitmap != null) {
            remoteViews.setImageViewBitmap(R.id.notification_icon, mParam.leftIconBitmap);
        }
        switch (mParam.viewType) {
            case UIType.TITLE_WITH_CONTENT_GREEN_BTN:
                return createTitleContentGreenBtn(remoteViews);
            default:
                break;
        }
        return remoteViews;
    }


    /**
     * TITLE_WITH_CONTENT_GREEN_BTN
     * TITLE_WITH_FULL_ICON
     */
    @SuppressLint("NewApi")
    private RemoteViews createTitleContentGreenBtn(RemoteViews remoteViews) {
        if (CompatibilityHelper.IsAndroid4_1()) {
            float textSize = CleanApplication.getInstance().getResources().getDimension(R.dimen.notification_button_textsize);
            remoteViews.setTextViewTextSize(R.id.notification_button, TypedValue.COMPLEX_UNIT_PX, textSize);
        }
        remoteViews.setTextViewText(R.id.notification_button, mParam.btnText);
        remoteViews.setTextViewText(R.id.notification_content, mParam.content);
        remoteViews.setTextViewText(R.id.notification_icon_text, mParam.iconText);
        if (mParam.iconTextColor != 0) {
            remoteViews.setTextColor(R.id.notification_icon_text, mParam.iconTextColor);
        }
        if (mParam.contentColor != 0) {
            remoteViews.setTextColor(R.id.notification_content, mParam.contentColor);
        }
        return remoteViews;
    }


}
